package com.hsgene.product.service.impl;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.UsedType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.generator.dto.GeneratorDto;
import com.hsgene.product.condition.CommodityCategoryCondition;
import com.hsgene.product.domain.GeneticCommodityCategory;
import com.hsgene.product.dto.GeneticCommodityCategoryDto;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.persistence.CommodityRepository;
import com.hsgene.product.persistence.GeneticCommodityCategoryMapper;
import com.hsgene.product.service.CommodityCategoryService;
import com.hsgene.product.service.api.GeneratorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/10/15 16:57
 */
@Service
public class CommodityCategoryServiceImpl implements CommodityCategoryService {
    @Autowired
    GeneratorService generatorService;
    @Autowired
    GeneticCommodityCategoryMapper geneticCommodityCategoryMapper;

    @Autowired
    private CommodityRepository commodityRepository;

    @Override
    public ApiResult addCategory(GeneticCommodityCategory commodityCategory) {
        GeneticCommodityCategory commodityCategoryByName=geneticCommodityCategoryMapper.getByName(commodityCategory);
        if (commodityCategoryByName!=null) {
            return ApiResult.fail(ProductErrorCode.THIS_CATEGORY_ALREADY_EXISTS);
        }
        ApiResult<GeneratorDto> generator = generatorService.getGenerator(null, null);
        if(!generator.isSucc()){
            return ApiResult.fail(generator.getError());
        }
        commodityCategory.setId(generator.getData().getSerialNumber());
        commodityCategory.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
        commodityCategory.setAmount(UsedType.NO_USED.getCode());
        geneticCommodityCategoryMapper.insertSelective(commodityCategory);
        return ApiResult.succ(commodityCategory);
    }

    @Override
    public ApiResult<GeneticCommodityCategory> getById(String id) {
        GeneticCommodityCategory geneticCommodityCategory = geneticCommodityCategoryMapper.selectByPrimaryKey(id);
        return ApiResult.succ(geneticCommodityCategory);
    }

    @Override
    public ApiResult updateCategory(GeneticCommodityCategory commodityCategory) {
        GeneticCommodityCategory commodityCategoryByName=geneticCommodityCategoryMapper.getByName(commodityCategory);
        if (commodityCategoryByName!=null&&!commodityCategory.getId().equals(commodityCategoryByName.getId())) {
            return ApiResult.fail(ProductErrorCode.THIS_CATEGORY_ALREADY_EXISTS);
        }
        geneticCommodityCategoryMapper.updateByPrimaryKeySelective(commodityCategory);
        // fix[19706] 冗余字段没有同步更新
        commodityRepository.updateCategoryName(commodityCategory.getId(), commodityCategory.getCategory());
        return ApiResult.succ();
    }

    @Override
    public ApiResult deletecory(String id) {
        GeneticCommodityCategory geneticCommodityCategory = new GeneticCommodityCategory();
        geneticCommodityCategory.setDeleteFlag(DeleteFlagType.DELETE.getCode());
        geneticCommodityCategory.setId(id);
        GeneticCommodityCategory geneticCommodityCategory1 = geneticCommodityCategoryMapper.selectByPrimaryKey(id);
        if(geneticCommodityCategory1.getAmount()>0){
            return ApiResult.fail(ProductErrorCode.CANNOT_BE_DELETED_AFTER_BEING_USED);
        }
        geneticCommodityCategoryMapper.updateByPrimaryKeySelective(geneticCommodityCategory);
        return ApiResult.succ();
    }

    @Override
    public ApiResult queryCategory(CommodityCategoryCondition condition) {
        int totalCount=geneticCommodityCategoryMapper.queryCategoryCount(condition);
        Pagination<GeneticCommodityCategoryDto> pagination = Pagination.newInstance(condition.getLimitHelper(), totalCount);
        int rowNumber = (condition.getLimitHelper().getPage() - 1) * condition.getLimitHelper().getSize();
        if(totalCount>0){
            List<GeneticCommodityCategory> list= geneticCommodityCategoryMapper.queryCategory(condition);
            List<GeneticCommodityCategoryDto> categoryDtoList=new ArrayList<>();
            for (int i = 0, n = list.size(); i < n; i++) {
                GeneticCommodityCategory geneticCommodityCategory = list.get(i);
                GeneticCommodityCategoryDto geneticCommodityCategoryDto = new GeneticCommodityCategoryDto();
                BeanUtils.copyProperties(geneticCommodityCategory,geneticCommodityCategoryDto);
                geneticCommodityCategoryDto.setNumber(rowNumber + i + 1);
                categoryDtoList.add(geneticCommodityCategoryDto) ;
            }
            pagination.setResult(categoryDtoList);
        }else{
            pagination.setResult(new ArrayList<>());
        }
        return ApiResult.succ(pagination);
    }

    @Override
    public ApiResult getAll() {
        List<GeneticCommodityCategory> list=geneticCommodityCategoryMapper.getAll();
        return ApiResult.succ(list);
    }

    @Override
    public ApiResult getByName(String category) {
        GeneticCommodityCategory geneticCommodityCategory = new GeneticCommodityCategory();
        geneticCommodityCategory.setCategory(category);
        GeneticCommodityCategory byName = geneticCommodityCategoryMapper.getByName(geneticCommodityCategory);
        return ApiResult.succ(byName);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult<NullObject> updateCategoryUsedCount(String categoryId, boolean isAddition) {
        geneticCommodityCategoryMapper.updateCategoryUsedCount(categoryId, isAddition);
        return ApiResult.succ();
    }
}
